package learn.java.basic.util.zip;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class CompressUseGzipStream {


    public static void main(String[] args) throws IOException {
        String s = "随着时序入深秋，高纬度地区气温逐渐下降，芬兰海卢奥托岛（Hailuoto）海滩日前罕见出现成千上万、大小相当的“冰蛋”。那么，芬兰海滩这些万颗冰蛋是怎么形成的？会融化吗";
        File file = File.createTempFile("gzip", ".txt");
        System.out.println(file.getAbsolutePath());
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);
        gzipOutputStream.write(s.getBytes());
        gzipOutputStream.flush();
        gzipOutputStream.close();

        FileInputStream fileInputStream = new FileInputStream(file);
        GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
        byte[] bytes = new byte[8];

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int l = 0;
        while ((l = gzipInputStream.read(bytes)) > 0) {
            byteArrayOutputStream.write(bytes, 0, l);
        }
        System.out.println(new String(byteArrayOutputStream.toByteArray()));

    }
}
